package com.cenwangkeji.resident.presenter.activity;

import android.text.TextUtils;

import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.ImgBean;
import com.cenwangkeji.resident.model.bean.QuestionDetailBean;
import com.cenwangkeji.resident.model.bean.SuccessBean;
import com.cenwangkeji.resident.model.bean.User;
import com.google.gson.Gson;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * @松 on 2019/11/7 14:11
 * 问题详情
 */
public class QuestionDetailPresenter {
    private I mI;

    public QuestionDetailPresenter(I i) {
        mI = i;
    }

     //获取聊天内容
    public void getQuestion(final int id){
        Observable.create(new ObservableOnSubscribe<QuestionDetailBean>() {
            @Override
            public void subscribe(ObservableEmitter<QuestionDetailBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put( Constant.token, User.getToken());
                map.put( "xywyId",id);
                map.put( "mark",3);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.question_detail);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, QuestionDetailBean.class));
            }
        }).compose(RxUtils.<QuestionDetailBean>io_main()).subscribe(new ObserverOnNext<QuestionDetailBean>() {
            @Override
            public void onNext(QuestionDetailBean questionDetailBean) {
                if (mI !=null){
                    mI.onQuestionSuccess(questionDetailBean);
                }

            }
        });

    }

    public void send(final String path, final String content, final int id){

        Observable.create(new ObservableOnSubscribe<SuccessBean>() {
            @Override
            public void subscribe(ObservableEmitter<SuccessBean> emitter) throws Exception {
                Gson gson = new Gson();
                int image =0;
                Map<Object,Object> map = new HashMap<>();
                if (!TextUtils.isEmpty(path)){
                    File file = new File(path);
                    String json = OkHttpUtils.uploadImage(Constant.image_url,file);
                    ImgBean imgBean = gson.fromJson(json, ImgBean.class);
                    image = imgBean.getFileUrl().get(0);
                }
              /*  map.put("picid1",0);
                map.put("picid2",0);
                map.put("picid3",0);
                map.put("picid4",0);
                map.put("picid5",0);
                map.put("picid6",0);*/
                if (image !=0){
                    map.put("picid1",image);
                }
                map.put("yxwyId",id);
                map.put("wdnr",content);
                map.put("wdType",1);
                map.put( Constant.token, User.getToken());
                map.put( "mark",3);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.question_send);
                MyLogger.i(json);
                SuccessBean successBean  = gson.fromJson(json, SuccessBean.class);
                emitter.onNext(successBean);

            }
        }).compose(RxUtils.<SuccessBean>io_main()).subscribe(new ObserverOnNext<SuccessBean>() {
            @Override
            public void onNext(SuccessBean bean) {
               if (mI != null){
                   mI.onSendSuccess(bean);
               }
            }
        });


    }

    public interface  I{
         void onQuestionSuccess(QuestionDetailBean bean);
         void onSendSuccess(SuccessBean bean);
    }

}
